package com.vinyl.components 
{	
	import fl.core.UIComponent;
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import com.vinyl.contents.SimpleFlv;
	
	public class SimpleFlvPlayer extends UIComponent
	{	
		public var simpleFlv = new SimpleFlv();
		public var fullScreenMode = false;
		public var flvUrl = "";
		public var simpleFlvPlayerUi;
		public var bg:FlvBg;
		
		
		private var _autoSize;
		
		public function set autoSize( $value ): void {
			_autoSize = $value;
			simpleFlvPlayerUi.autoSize = _autoSize;
		}
		public function get autoSize(): Boolean {
			return _autoSize;
		}
		
		/**
		 * constructor
		 * @param SimpleFlvPlayer ( void )
		 */
		public function SimpleFlvPlayer( $url:String = "", $autoSize:Boolean = false )
		{
			super();
			trace( $url );
			bg = new FlvBg();
			if ( $url != "" ) flvUrl = $url;
			simpleFlvPlayerUi = new SimpleFlvPlayerUi( this );
			autoSize = $autoSize;
			init();
		}
		
		override protected function configUI():void
		{
			super.configUI();
		}		
		
		override protected function draw():void
		{
			super.draw();
		}
		
		public function init( ): void
		{
			addChild( bg );
			addChild( simpleFlv );
			addChild( simpleFlvPlayerUi );
			simpleFlvPlayerUi.x = 20;
			simpleFlvPlayerUi.y = 310;
			
			simpleFlv.addEventListener( "playBackComplete", function(e) {
				//simpleFlv.removeEventListener( "playBackComplete", arguments.callee );
				simpleFlv._stream.seek( 0 * simpleFlv.dur / 100 );
				simpleFlvPlayerUi.playBtn.gotoAndStop( 1 );
				simpleFlv.pause();
				
				( e.currentTarget.parent as Object ).dispatchEvent( new Event( "playBackComplete" ) );
			} );
			
			simpleFlv.addEventListener( "load", function(e)
			{
				simpleFlv.width = 640;
				simpleFlv.height = 360;
				simpleFlv.removeEventListener( "load", arguments.callee );
				stage.dispatchEvent( new Event( Event.RESIZE ) );
			} );
			
			addEventListener( Event.ENTER_FRAME, enterFrameHandler );
			addEventListener( Event.ENTER_FRAME, progressFrameHandler ); 
			
			addEventListener( Event.REMOVED_FROM_STAGE, function(e)
			{ 
				removeEventListener( Event.ENTER_FRAME, enterFrameHandler );
				removeEventListener( Event.ENTER_FRAME, progressFrameHandler );
				removeEventListener( Event.REMOVED_FROM_STAGE,  arguments.callee );
			} );
			
			simpleFlvPlayerUi.pauseBtn.addEventListener( MouseEvent.CLICK, click )
			simpleFlvPlayerUi.playBtn.addEventListener( MouseEvent.CLICK, click )
			simpleFlvPlayerUi.seekBar.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			simpleFlvPlayerUi.pauseBtn.gotoAndStop( 1 );
			simpleFlvPlayerUi.totalTime.text = ""
			
			simpleFlvPlayerUi.playBtn.gotoAndStop( 1 );
		}
		
		private function enterFrameHandler( event ) 
		{
			var per =  Math.round( simpleFlv._stream.time * 100 / simpleFlv.dur ) / 100;
			
			simpleFlvPlayerUi.pg_bar.width = Math.round( simpleFlvPlayerUi.seekBar.width * per ); 
			simpleFlvPlayerUi.cTime.text = checkTime( simpleFlv._stream.time );
			simpleFlvPlayerUi.totalTime.text = Math.floor( simpleFlv.dur / 60 ) + ":" + Math.round( simpleFlv.dur % 60 )
			var buffer:Number = Math.min( Math.round( simpleFlv._stream.bufferLength / simpleFlv._stream.bufferTime * 100 ), 100 ); 
		}
		
		private function progressFrameHandler( event ) 
		{
			var bl = Math.round( simpleFlv._stream.bytesLoaded/1000 );
			var bt = Math.round( simpleFlv._stream.bytesTotal/1000 );
			var per = Math.round ( 100 * simpleFlv._stream.bytesLoaded  / simpleFlv._stream.bytesTotal ) / 100;  
			
			simpleFlvPlayerUi.pgDownload_bar.width = Math.round( simpleFlvPlayerUi.seekBar.width * per );
		}
		
		private function checkTime( t ) :String
		{
			var ns_seconds: Number
			
			if( t == undefined )
				ns_seconds = simpleFlv._stream.time;
			else
				ns_seconds = t;
				
			var minutes: Number = Math.floor( ns_seconds / 60 );
			var seconds = Math.floor( ns_seconds % 60 );
			if ( seconds < 10 ) 
			{
				seconds = "0" + seconds;
			}
			return minutes + ":" + seconds;
		}
		
		private function getWidthRate( mc:MovieClip, maxNum: Number ): Number
		{
			var value: Number = 100;
			var max = maxNum;
			var mp = mc.parent.mouseX - mc.x;
			if( mp < 0 )
			{
				value =  0;
			}
			else if( mp > max)
			{
				value =  100;
			}
			else if( mp > 0 && mp < max )
			{
				value = Math.round( ( 100 * mp ) / max );		
			}
			return value;
		}
		
		private function click(e)
		{
			switch( e.currentTarget.name )
			{
				case "pauseBtn": 
				
					if ( !simpleFlv.initFlag ) //초기화 안되었을때
					{
						simpleFlvPlayerUi.playBtn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
					}
					else
					{
						e.currentTarget.toggle = !e.currentTarget.toggle;
						if ( e.currentTarget.toggle )
						{
							simpleFlv._stream.resume();
							simpleFlvPlayerUi.playBtn.gotoAndStop( 2 );
							simpleFlvPlayerUi.pauseBtn.gotoAndStop( 2 );
						}
						else
						{
							simpleFlv._stream.pause();
							simpleFlvPlayerUi.playBtn.gotoAndStop( 1 );
							simpleFlvPlayerUi.pauseBtn.gotoAndStop( 1 );
						}
					}
				break;
				
				case "playBtn" :
						if ( !simpleFlv.initFlag ) //초기화 안되었을때
						{
							simpleFlv.play( flvUrl );
							simpleFlvPlayerUi.playBtn.gotoAndStop( 2 );
							simpleFlvPlayerUi.pauseBtn.gotoAndStop( 2 );
							simpleFlvPlayerUi.pauseBtn.toggle = true;
						}
						else
						{
							simpleFlvPlayerUi.pauseBtn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
						}
					
				break;
			}
		}
		
		private function mouseDownHandler( event: MouseEvent ): void 
		{
			if( event.target != simpleFlvPlayerUi.seekBar  ) return;
			
			var sprite = event.target;
			
			var num = getWidthRate( sprite, sprite.width );
			var per = Math.round ( 100 * simpleFlv._stream.bytesLoaded  / simpleFlv._stream.bytesTotal ); 
			
			if ( num > per ) num = per;
			
			simpleFlv._stream.seek( num * simpleFlv.dur / 100 );
			
			stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove_ );
			stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp_ );
		}
		
		private function onMouseMove_( event: MouseEvent ): void
		{
			if( event.target != simpleFlvPlayerUi.seekBar  ) return;
			var sprite = event.target;
			
			var num = getWidthRate( sprite, sprite.width );
			var per = Math.round ( 100 * simpleFlv._stream.bytesLoaded  / simpleFlv._stream.bytesTotal ); 
			
			if( num > per) num = per;
			simpleFlv._stream.seek( num * simpleFlv.dur / 100);
		}
		
		private function onMouseUp_( event: MouseEvent ): void
		{
			stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove_ );
			stage.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp_ );
		}
	}
}








